package cn.aixuxi.notebank.service.impl;

import cn.aixuxi.notebank.dto.NoteInputDTO;
import cn.aixuxi.notebank.dto.NoteOutputDTO;
import cn.aixuxi.notebank.entity.NoteEntity;
import cn.aixuxi.notebank.entity.UpdateRecordEntity;
import cn.aixuxi.notebank.mapper.NoteMapper;
import cn.aixuxi.notebank.service.NoteService;
import cn.aixuxi.notebank.service.UpdateRecordService;
import cn.aixuxi.notebank.util.Markdown2HtmlUtils;
import cn.hutool.core.date.DateUtil;
import com.alibaba.fastjson.JSON;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.cache.annotation.Cacheable;
import org.springframework.data.redis.core.StringRedisTemplate;
import org.springframework.stereotype.Service;
import org.springframework.util.StringUtils;

import java.util.Date;
import java.util.List;

/**
 * 类描述 笔记实现类
 *
 * @author ruozhuliufeng
 * @version 1.0
 * @date 2020/10/25 0:21
 */
@Service
public class NoteServiceImpl extends ServiceImpl<NoteMapper, NoteEntity> implements NoteService {

    @Autowired
    StringRedisTemplate redisTemplate;
    @Autowired
    UpdateRecordService updateRecordService;


    /**
     * 功能描述: 添加笔记
     *
     * @param noteInputDTO 笔记内容
     * @return : void
     * @author : ruozhuliufeng
     * @date : 2020/10/25 0:22
     */
    @Override
    public void save(NoteInputDTO noteInputDTO) {
        NoteEntity noteEntity = new NoteEntity();
        BeanUtils.copyProperties(noteInputDTO,noteEntity);
        noteEntity.setHtmlContent(Markdown2HtmlUtils.markdownToHtml(noteInputDTO.getMdContent()));
        noteEntity.setCreateTime(DateUtil.today());
        noteEntity.setCreateBy(noteInputDTO.getUsername());
        noteEntity.setUpdateTime(new Date());
        noteEntity.setUpdateBy(noteInputDTO.getUsername());
        this.save(noteEntity);
    }

    @Override
    public List<NoteOutputDTO> noteList() {
        return this.getBaseMapper().noteList();
    }

    /**
     * 功能描述: 根据ID获取详情
     *
     * @param id 笔记id
     * @return : cn.aixuxi.notebank.dto.NoteOutputDTO
     * @author : ruozhuliufeng
     * @date : 2020/10/25 1:03
     */
    @Override
    public NoteOutputDTO findById(Integer id) {
        NoteEntity noteEntity = this.getBaseMapper().selectById(id);
        NoteOutputDTO note = new NoteOutputDTO();
        BeanUtils.copyProperties(noteEntity,note);
        // 查看笔记详情时，当前浏览量+1
        noteEntity.setEyes(noteEntity.getEyes() + 1);
        this.getBaseMapper().updateById(noteEntity);
        return note;
    }

    /**
     * 功能描述: 修改笔记内容
     *
     * @param noteInputDTO 笔记内容
     * @param user         保存修改记录
     * @return : void
     * @author : ruozhuliufeng
     * @date : 2020/10/25 11:11
     */
    @Override
    public void updateNote(NoteInputDTO noteInputDTO) {
        NoteEntity noteEntity = this.getBaseMapper().selectById(noteInputDTO.getId());
        BeanUtils.copyProperties(noteInputDTO,noteEntity);
        noteEntity.setHtmlContent(Markdown2HtmlUtils.markdownToHtmlExtensions(noteEntity.getMdContent()));
        this.getBaseMapper().updateById(noteEntity);
        // 保存修改记录
        UpdateRecordEntity updateRecordEntity = new UpdateRecordEntity();
        BeanUtils.copyProperties(noteEntity,updateRecordEntity);
        updateRecordEntity.setNoteId(noteEntity.getId());
        updateRecordEntity.setUpdateBy(noteInputDTO.getUsername());
        updateRecordEntity.setUpdateTime(new Date());
        updateRecordEntity.setRemark(noteInputDTO.getRemark());
        updateRecordService.save(updateRecordEntity);
    }

    /**
     * 功能描述: 获取前五名查看量最高的笔记信息
     *
     * @return : 笔记信息
     * @author : ruozhuliufeng
     * @date : 2020/10/29 22:36
     */
    @Cacheable(value = {"note"},key = "#root.method.name")
    @Override
    public List<NoteOutputDTO> rank() {
        List<NoteOutputDTO> list = this.getBaseMapper().rank();
        return list;
    }

    /**
     * 功能描述: 获取最新笔记
     *
     * @return : cn.aixuxi.notebank.dto.NoteOutputDTO
     * @author : ruozhuliufeng
     * @date : 2020/10/29 23:17
     */
    @Cacheable(value = {"note"},key = "#root.method.name")
    @Override
    public NoteOutputDTO newNote() {
        NoteOutputDTO noteOutputDTO;
        String newNote = redisTemplate.opsForValue().get("newNote");
        if (StringUtils.isEmpty(newNote)){
            // 缓存中没有数据
            noteOutputDTO = this.getBaseMapper().getNewNote();
            String note = JSON.toJSONString(noteOutputDTO);
            redisTemplate.opsForValue().set("newNote",note);
        }else {
            noteOutputDTO = JSON.parseObject(newNote, NoteOutputDTO.class);
        }
        return noteOutputDTO;
    }


}
